package com.xac.common.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * @author frank
 * 
 */
public class MailUtil {

	public static final String EMAIL_FROM_ONE = "ettest@yahoo.cn";
	public static final String EMAIL_FROM_TWO = "ettest001@yahoo.cn";
	public static final String EMAIL_FROM_THREE = "ettest002@yahoo.cn";
	public static final String EMAIL_FROM_FOUR = "ettest003@yahoo.cn";
	public static final String EMAIL_FROM_FIVE = "ettest004@yahoo.cn";
	public static final String EMAIL_FROM_SIX = "ettest005@yahoo.com.cn";
	public static final String EMAIL_FROM_SEVEN = "ettest006@yahoo.com.cn";
	public static final String EMAIL_FROM_EIGHT = "ettest007@yahoo.com.cn";
	public static final String EMAIL_FROM_NINE = "ettest008@yahoo.com.cn";
	public static final String EMAIL_FROM_TEN = "ettest009@yahoo.com.cn";
	public static final String SMTP_YAHOO_COM_CN = "smtp.mail.yahoo.com.cn";
	
	public static final String EMAIL_PASSWORD = "123456";

	public static void sendMail(final String from, final String[] toList, final String subject, final String content) throws Exception {
		Properties props = new Properties();
		props.put("mail.smtp.host", SMTP_YAHOO_COM_CN);
		props.put("mail.smtp.auth", "true");
		
		Session session = Session.getDefaultInstance(props, new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(from, EMAIL_PASSWORD);
			}
		});
		
		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(from));
		InternetAddress[] address = new InternetAddress[toList.length];
		for (int i = 0; i < toList.length; i++) {
			address[i] = new InternetAddress(toList[i]);
		}
		
		msg.setRecipients(Message.RecipientType.TO, address);
		Date current = new Date();
		msg.setSubject(StringUtil.getString(subject));
		
		Multipart mp = new MimeMultipart();
		MimeBodyPart mbpContent = new MimeBodyPart();
		mbpContent.setText(content);
		mp.addBodyPart(mbpContent);
		msg.setContent(mp);
		msg.setSentDate(current);
		
		Transport.send(msg, address);
	}
}
